package com.ruoyi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * 网络请求封装
 */
public class HttpClientUtil {
  protected static String token = "aDZnMTkwbXMzMHI4MnhjbzQwNnhrdjVsODNpODhtOTBpcDQ0";//网络请求token

  public static String doGet(String url) {
    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String resultString = "-1";
    CloseableHttpResponse response = null;
    try {
      // 创建http GET请求
      HttpGet httpGet = new HttpGet(url);
//            log.info(" ="+url);
      httpGet.setHeader("token", token);
      httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
      // 设置超时时间
      RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(30000).setSocketTimeout(30000).setConnectTimeout(60000).build();
      httpGet.setConfig(config);
      // 执行请求
      response = httpclient.execute(httpGet);
      // 判断返回状态是否为200
      if (response.getStatusLine().getStatusCode() == 200) {
        resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
      }
//            else{
//                log.info("code="+response.getStatusLine().getStatusCode());
//            }
    } catch (Exception e) {
    } finally {
      try {
        if (response != null) {
          response.close();
        }
        httpclient.close();
      } catch (IOException e) {
      }
    }
    return resultString;
  }


  public static String doPost(String url, Object json) {
    return doPostByToken(url, json, null);
  }

  public static String doPostByToken(String url, Object json, String access_token) {
    // 创建Httpclient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String resultString = "-1";
    try {
      // 创建Http Post请求
      HttpPost httpPost = new HttpPost(url);
      // 设置超时时间
      RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(30000).setSocketTimeout(30000).setConnectTimeout(60000).build();
      httpPost.setHeader("token", null == access_token ? token : access_token);
      httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
      httpPost.setConfig(config);
      // 创建请求内容
      if (null != json) {
        StringEntity entity = new StringEntity(JSON.toJSONString(json, SerializerFeature.NotWriteRootClassName, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.SkipTransientField), ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);
      }

      // 执行http请求
      response = httpClient.execute(httpPost);
      if (response.getStatusLine().getStatusCode() == 200) {
        resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
      }
    } catch (Exception e) {
    } finally {
      if (null != response) {
        try {
          response.close();
        } catch (IOException e) {
        }
      }
    }
    return resultString;
  }


}
